<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>{{#if name}}{{name}} &ndash; {{/if}}Solid Home</title>
  <link rel="stylesheet" href="/common/css/bootstrap.min.css">
  <link rel="stylesheet" href="/common/css/solid.css">
</head>
<body>
<div class="container">
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <a href="/" class="navbar-brand">{{#if name}}{{name}} &ndash; {{/if}}Solid Home</a>
      </div>
      <div class="nav navbar-right navbar-form">
        <button id="session-action" type="button" class="session-action btn btn-default">Loading...</button>
      </div>
    </div>
  </nav>

  <p class="lead">
    <span>
      This is a public homepage of {{#if name}}{{name}}, whose WebID is{{else}}a user with WebID{{/if}}
    </span>
    <a href="{{webId}}"><mark>{{webId}}</mark></a>.
  </p>

  <section>
    <h1>Data</h1>
    <div class="list-group">
      <a href="/profile/card" target="_blank" class="list-group-item">
        <span class="lead">Profile</span>
        <span class="badge">public</span>
      </a>
      <a href="/inbox/" target="_blank" class="list-group-item">
        <span class="lead">Inbox</span>
        <span class="badge">private</span>
      </a>
      <a href="/public/" target="_blank" class="list-group-item">
        <span class="lead">Public Folder</span>
        <span class="badge">public</span>
      </a>
      <a href="/private/" target="_blank" class="list-group-item">
        <span class="lead">Private Folder</span>
        <span class="badge">private</span>
      </a>
    </div>
  </section>

  <section>
    <h1>Apps</h1>
    <div class="list-group">
      <a href="https://github.com/solid/userguide" target="_blank" class="list-group-item">
        <span class="lead">Getting started with Solid and Data Browser</span>
      </a>
    </div>
  </section>

  <section class="hidden" id="account-settings">
    <h1>Account settings</h1>
    <div class="list-group">
      <a href="/account/delete/" class="list-group-item">
        <span class="lead">Delete account</span>
      </a>
    </div>
  </section>
</div>

<script src="/common/js/solid-auth-client.bundle.js"></script>
<script type="text/javascript">
  (function () {
    'use strict';
    var button = document.getElementById('session-action');
    var loggedIn = false;
    var accountSettings = document.getElementById('account-settings');

    solid.auth.trackSession(function(session) {
      loggedIn = !!session;
      button.innerText = loggedIn ? 'Log out' : 'Log in';
      button.classList.remove('btn-default');
      button.classList.remove('btn-danger');
      button.classList.remove('btn-primary');
      button.classList.add(loggedIn ? 'btn-danger' : 'btn-primary');
      if (loggedIn) {
        var sessionOrigin = getOriginFromWebId(session.webId);
        var isOwner = sessionOrigin === location.origin;
        accountSettings.classList.toggle('hidden', !isOwner);
      } else {
        accountSettings.classList.toggle('hidden', true);
      }
    });

    button.addEventListener('click', function () {
      loggedIn ? solid.auth.logout() : solid.auth.popupLogin();
    });

    function getOriginFromWebId(webId) {
      if (window.URL.prototype) {
        return new URL(webId).origin;
      }
      var uriParts = webId.split('/');
      return uriParts[0] + '//' + uriParts[2];
    }
  })();
</script>
</body>
</html>
